package com.gyms.view.bottomview;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import utils.AnimotionUtils;

/**
 * Created by ltx on 17/12/29
 */

public abstract class BaseBottomDialogFragment extends android.support.v4.app.DialogFragment {
    private View mDialogView;

    private boolean isAnimation = false;
    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM; // 显示在底部
        params.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度填充满屏
        window.setAttributes(params);
        // 这里用透明颜色替换掉系统自带背景
        int color = ContextCompat.getColor(getActivity(), android.R.color.transparent);
        window.setBackgroundDrawable(new ColorDrawable(color));
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题栏
        mDialogView = inflater.inflate(getLayoutRes(), container, false);
        initData();
        initView(mDialogView);
        initListener();
        startUpAnimation(mDialogView);
        return mDialogView;
    }

    public abstract int getLayoutRes();
    public abstract void initListener();
    public abstract void initData();
    public abstract void initView(View mView);


    private void startUpAnimation(View view) {
        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

        slide.setDuration(400);
        slide.setFillAfter(true);
        slide.setFillEnabled(true);
        view.startAnimation(slide);
    }

    @Override
    public void dismiss() {
        if (isAnimation) {
            return;
        }
        isAnimation = true;
        AnimotionUtils.slideToDown(mDialogView, new AnimotionUtils.AnimationListener() {
            @Override
            public void onFinish() {
                isAnimation = false;
                BaseBottomDialogFragment.super.dismiss();
            }
        });
    }
}


